package com.aisi.service.impl;

import com.aisi.domain.ResponseResult;
import com.aisi.service.UploadService;
import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * @Author: shenjianZ
 * @Date: 2024/3/26 21:16
 * @Description:
 */

@Service
public class UploadServiceImpl implements UploadService {

    private String accessKey;
    private String secretKey;
    private String bucket;

    public void setAccessKey(String accessKey) {
        this.accessKey = accessKey;
    }

    public void setSecretKey(String secretKey) {
        this.secretKey = secretKey;
    }

    public void setBucket(String bucket) {
        this.bucket = bucket;
    }

    @SneakyThrows
    @Override
    public ResponseResult uploadImg(MultipartFile file) {
        String accessKey = "TqV1I7Q7n6ES2lYo8s0PIJ2wSs94YzCbtTNQNwi1";
        String secretKey = "w5q8qxvYRRGepl6oKOgcArlZQJIKOqBA0-v41RvO";
        String bucket = "shenjianl";
        String url = uploadOS(file);
        return ResponseResult.okResult(url);
    }
    private String uploadOS(MultipartFile file) throws FileNotFoundException {
        //构造一个带指定 Region 对象的配置类
        Configuration cfg = new Configuration(Region.autoRegion());
        cfg.resumableUploadAPIVersion = Configuration.ResumableUploadAPIVersion.V2;// 指定分片上传版本


        UploadManager uploadManager = new UploadManager(cfg);
        //...生成上传凭证，然后准备上传


        //默认不指定key的情况下，以文件内容的hash值作为文件名
        String key = "2024";

        //            byte[] uploadBytes = "hello qiniu cloud".getBytes("utf-8");
        FileInputStream fileInputStream = new FileInputStream("D:\\JetBrainsToolProject\\InteliJIDEA\\baseBlog\\.gitignore");
        Auth auth = Auth.create(accessKey, secretKey);
        String upToken = auth.uploadToken(bucket);

        try {
            Response response = uploadManager.put(fileInputStream, key, upToken, null, null);
            //解析上传成功的结果
            DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
//            System.out.println(putRet.key);
//            System.out.println(putRet.hash);
            return "http://p917285360.oss-cn-shenzhen.aliyuncs.com/" + putRet.key;
        } catch (QiniuException ex) {
            ex.printStackTrace();
            if (ex.response != null) {
                System.err.println(ex.response);

                try {
                    String body = ex.response.toString();
                    System.err.println(body);
                } catch (Exception ignored) {
                }
            }
        }
        return null;
    }

}
